/***************************
新增or编辑
* */
<template>
  <el-dialog
    :title="dialogTitle"
    :close-on-click-modal="false"
    :visible.sync="visible"
    top="8vh"
    @close="closeDialog()"
    width="40vw"
  >
    <div class="boxsty">
      <el-form
        ref="dataForm"
        :rules="formRules"
        :model="dataForm"
        label-width="100px"
        @keyup.enter.native="dataFormSubmit()"
      >
        <el-row>
          <el-form-item label="统计名称:" prop="hzzqid">
            <el-select
              v-model="dataForm.hzzqid"
              placeholder="请选择统计名称"
              class="xiaLa"
              style="width: 100%"
              :clearable="true"
              @change="selecteName"
            >
              <el-option
                v-for="(item, index) in tjmcsList"
                :key="index"
                :label="item.name"
                :value="String(item.value)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-row>
      <!--  <div ><span style="color: #b6202b;margin-left: 10px;">*</span><span style="font-size: 16px;color: #303745;margin-left: 2px;">规则名称：</span>
        <div style="float:right;width:568px;">-->
          <el-form-item  label="规则名称:" prop="ajszb" >
          <el-input type="text" v-model="dataForm.ajszb" placeholder="规则名称1"  max-length="10"></el-input>
        </el-form-item>
        <el-form-item  prop="aszb">
          <el-input type="text" v-model="dataForm.aszb" placeholder="规则名称2"  max-length="10"></el-input>
        </el-form-item>
        <el-form-item  prop="bszb">
          <el-input type="text" v-model="dataForm.bszb" placeholder="规则名称3"  max-length="10"></el-input>
        </el-form-item>
        <el-form-item  prop="cszb">
          <el-input type="text" v-model="dataForm.cszb" placeholder="规则名称4"  max-length="10"></el-input>
        </el-form-item>
        <el-form-item  prop="dszb">
          <el-input type="text" v-model="dataForm.dszb" placeholder="规则名称5"  max-length="10"></el-input>
        </el-form-item>
        <el-form-item  prop="eszb">
          <el-input type="text" v-model="dataForm.eszb" placeholder="规则名称6"  max-length="10"></el-input>
        </el-form-item>
        <el-form-item  prop="fszb">
          <el-input type="text" v-model="dataForm.fszb" placeholder="规则名称7"  max-length="10"></el-input>
        </el-form-item>
      <!--  </div>
        </div>-->
        <el-form-item label="备注:" prop="bz">
          <el-input type="textarea" :rows="4" v-model="dataForm.bz" placeholder="备注"  max-length="10"></el-input>
        </el-form-item>

      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" plain @click="closeDialog()">返回</el-button>
      <el-button type="primary" plain @click="handleConfirm">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
// 接口 api
import {
    getHzzq,
    addPfhzbmhz,
    updatePfhzbmhz,
    getName
} from "@/api/statisticsanalysis/pfhzbmhzApi.js";
export default {
  data() {
    return {
      dialogTitle: "新增",
      visible: false,
      dataForm: {

      },
      tjmcsList:[],
      hzzqmc:"",
      formRules: {
        hzzqid: [{ required: true, message: "请选择统计名称", trigger: "change" }],
        bz: [{ required: true, message: "请输入备注", trigger: "blur" }],
        ajszb: [{ required: true, message: "请输入规则名称1", trigger: "blur" }],
        aszb: [{ required: true, message: "请输入规则名称2", trigger: "blur" }],
        bszb: [{ required: true, message: "请输入规则名称3", trigger: "blur" }],
        cszb: [{ required: true, message: "请输入规则名称4", trigger: "blur" }],
        dszb: [{ required: true, message: "请输入规则名称5", trigger: "blur" }],
        eszb: [{ required: true, message: "请输入规则名称6", trigger: "blur" }],
        fszb: [{ required: true, message: "请输入规则名称7", trigger: "blur" }],

      },

    };
  },
  created() {

  },
  methods: {

    // 初始化
    init(data,tabType) {

      if (data) {
        this.dataForm = data;
        this.dialogTitle = "编辑";
        this.getXgHzzq(data.hzzqmc);//周期
      }else{
         this.dataForm.type = tabType ;
         this.getHzzq();
      }

      this.visible = true;
    },
      getHzzq(){
          getHzzq().then(res => {
              res.data.data.map(it => {
                  let item = {
                      name:it.tjmc,
                      value:it.id
                  }

                  this.tjmcsList.push(item)
              });

          });
      },
      getXgHzzq(obj){
          getHzzq().then(res => {

              res.data.data.map(it => {
                  let item = {
                      name:it.tjmc,
                      value:it.id
                  }

                  this.tjmcsList.push(item)

              });
              this.hzzqmc = obj
          });
      },
      selecteName(obj){

          if(obj != "" && obj != null && obj != undefined){
              getName(obj).then(r => {

                  this.hzzqmc = r.data.data.tjmc;
                  this.$set(this.dataForm,'ajszb',r.data.data.ajszb);
                  this.$set(this.dataForm,'aszb',r.data.data.aszb);
                  this.$set(this.dataForm,'bszb',r.data.data.bszb);
                  this.$set(this.dataForm,'cszb',r.data.data.cszb);
                  this.$set(this.dataForm,'dszb',r.data.data.dszb);
                  this.$set(this.dataForm,'eszb',r.data.data.eszb);
                  this.$set(this.dataForm,'fszb',r.data.data.fszb);



              })

          }else{


          }


      },
    // 确定
    handleConfirm() {

        this.$refs.dataForm.validate(valid => {
            if (valid) {
                this.dataForm['hzzqmc'] = this.hzzqmc;
                if (this.dataForm.id) {
                    updatePfhzbmhz(Object.assign(this.dataForm))
                        .then(r => {
                            if (r.data.code == 0) {
                                this.$message.success("编辑成功");
                                this.$emit("refreshDataList");
                                this.closeDialog();
                            } else {
                                this.$message.error(r.data.msg);
                            }
                        })
                        .catch(err => {});
                } else {
                    addPfhzbmhz(Object.assign(this.dataForm))
                        .then(r => {
                            if (r.data.code == 0) {
                                this.$message.success("新增成功");
                                this.$emit("refreshDataList");
                                this.closeDialog();
                            } else {
                                this.$message.error(r.data.msg);
                            }
                        })
                        .catch(err => {});
                }
            } else {
                console.log("error submit!!");
                return false;
            }
        });
    },
    // 表单提交
    dataFormSubmit() {},
    // 关闭
    closeDialog() {
      this.visible = false;
      this.$emit("closeDialog");
    }
  }
};
</script>

<style  lang="scss" scoped>

</style>
